﻿'// ========================================================================= '
'// ------------------------------------------------------------------------- '
'//!				\file		CM_Polyline.vb
'			        \date		june 2010
'			        \author		TNick
'
'			        \brief		
'
'
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'   This file is considered universal knowledge. No restrictions
'   are imposed; use as you see fit.
'   Please read ReadMe.txt and Licence.txt in root folder
'   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'
'   
'
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
'//
'//
'//
'//
'//  IMPORTS     ------------------------------------------------------------ '
#Region "Imports"

Imports System
Imports System.IO
Imports Microsoft.Win32
Imports System.Xml.Serialization

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Customization
Imports Autodesk.AutoCAD.PlottingServices
Imports Autodesk.AutoCAD.Colors

#End Region
'//  IMPORTS     ============================================================ '
'//
'//
'//
'//
'//  DEFINITIONS    --------------------------------------------------------- '
<Assembly: CommandClass(GetType(CM_Polyline))> 



#Region "Public Structures"


'// --------------------------------------------------------------------- '
'// defines the settings for this module
Public Structure POLYSTG

    Public place_holder As Integer

End Structure
'// ===================================================================== '



#End Region



'//  DEFINITIONS    ========================================================= '
'//
'//
'//
'//
'//  DATA    ---------------------------------------------------------------- '


'//  DATA    ================================================================ '
'//
'//
'//
'//
'//  CLASS    --------------------------------------------------------------- '

Public Class CM_Polyline
    Inherits CModule


    '// --------------------------------------------------------------------- '
    '// DATA
#Region "Public Constants"
    '// name as displayed in various places
    Public Const sAPPNAME As String = "Polyline"
    '// version as displayed in various places
    Public Const sAPPVER As String = "1.0.0.0"

    '// Command. Brings up the settings dialog
    Public Const sCMD_SETTINGS As String = "TNIK_POLY_STG"
    '// Command. Inserts a point in a line
    Public Const sCMD_INSP_L As String = "TNIK_POLY_INSPL"
    '// Command. Inserts multiple points between two other points
    Public Const sCMD_INSP_2P As String = "TNIK_POLY_INSP2"
    '// Command. Select all objects inside the polyline
    Public Const sCMD_SEL As String = "TNIK_POLY_SEL"
    '// Command. Eliminate points from polyline
    Public Const sCMD_DELP As String = "TNIK_POLY_DELP"
    '// Command. Sharpen a polyline (reduce point number)
    Public Const sCMD_SHARP As String = "TNIK_POLY_SHARP"
    '// Command. Blur a polyline (increase point number)
    Public Const sCMD_BLUR As String = "TNIK_POLY_BLUR"
#End Region

#Region "Public Data"
    '// short description
    Public Shared sAPPTIP As String
    '// the settings
    Public Shared Stgs As POLYSTG

#End Region
    '// ===================================================================== '

#Region "Private general purpose Functions"


    '// --------------------------------------------------------------------- '
    '// This function saves current settings
    Private Shared Sub SaveSett()
        Try

        Catch ex As System.Exception
            Debug.Print("Polyline SaveSett Exception " & ex.Message)
        End Try
        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Sub
    '// ===================================================================== '


    '// --------------------------------------------------------------------- '
    '// This function loads current settings
    Private Shared Sub GetSett()
        Try

        Catch ex As System.Exception
            Debug.Print("Polyline GetSett Exception " & ex.Message)
        End Try
        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Sub
    '// ===================================================================== '


    '// --------------------------------------------------------------------- '
    '// This function shows current settings
    Private Shared Function PrintStg(ByRef lcStg As POLYSTG) As String
        Return (vbNewLine & CMain._T(TX.CSTG) & sAPPNAME & ": " & vbNewLine & _
 _
              CMain._T(TX.MODEL) & ":" & vbNewLine & _
          vbNewLine & vbNewLine)

    End Function
    '// ===================================================================== '

#End Region

#Region "Common interface" ' -----------------------

    '// --------------------------------------------------------------------- '
    '// a new instance is created
    Public Sub New()
        Try
            sAPPTIP = CMain._T(TX.POLY_TIP)
            Return
        Catch ex As System.Exception
            CMain.DebugException(ex)
            Return
        End Try
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    '// the module is asked to terminate
    Public Overrides Function Terminate() As Boolean
        Try
            SaveSett()
            Return True
        Catch ex As System.Exception
            CMain.DebugException(ex)
            Return False
        End Try
    End Function
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    '// the module is informed about application start-up
    Public Shared Sub DoPrereq()
        Try
            ' this function is called at application start-up in order to 
            ' allow applications to do any prerequisites
            ' there is no instance of this class at this time
            GetSett()
        Catch ex As System.Exception
            CMain.DebugException(ex)
        End Try
        Return
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    '// create the user interface for this module
    Public Shared Sub DoCUI( _
     ByRef pcs As CustomizationSection, _
     ByRef mg As MacroGroup)
        ' this function is called when a CUI is builded in order to
        ' allow any application specifics
        Try
#If HARDCORE_CUI Then
            If CApp.cuiCl Is Nothing Then Exit Try
            CApp.cuiCl.AddMacro(sCMD_INSP_L, "POLY_Ins1")
            CApp.cuiCl.AddMacro(sCMD_INSP_2P, "POLY_InsM")
            CApp.cuiCl.AddMacro(sCMD_DELP, "POLY_DelP")
            CApp.cuiCl.AddMacro(sCMD_SHARP, "POLY_Sharp")
            CApp.cuiCl.AddMacro(sCMD_BLUR, "POLY_Blur")
            CApp.cuiCl.AddMacro(sCMD_SEL, "POLY_Sel")
            CApp.cuiCl.AddMacro(sCMD_SETTINGS, "POLY_Settings")

            CApp.cuiCl.AddToolbar( _
                sAPPNAME, _
                Application.MainWindow.Location.X, _
                100 + MIDS.POLY * 30)
            CApp.cuiCl.AddToolbarBtn(CMain._T(TX.PLY_INSP), sCMD_INSP_L)
            CApp.cuiCl.AddToolbarBtn(CMain._T(TX.PLY_INSMP), sCMD_INSP_2P)
            CApp.cuiCl.AddToolbarSep()
            CApp.cuiCl.AddToolbarBtn(CMain._T(TX.PLY_DELP), sCMD_DELP)
            CApp.cuiCl.AddToolbarSep()
            CApp.cuiCl.AddToolbarBtn(CMain._T(TX.PLY_SHARP), sCMD_SHARP)
            CApp.cuiCl.AddToolbarBtn(CMain._T(TX.PLY_ROUND), sCMD_BLUR)
            CApp.cuiCl.AddToolbarSep()
            CApp.cuiCl.AddToolbarBtn(CMain._T(TX.PLY_SELOBJINS), sCMD_SEL)
            CApp.cuiCl.AddToolbarSep()
            CApp.cuiCl.AddToolbarBtn(CMain._T(TX.SETTINGS), sCMD_SETTINGS)

#Else
			Dim mm1 As MenuMacro = Nothing
			mm1 = New MenuMacro(mg, sCMD_SEL, "^C^C" & sCMD_SEL, "ID_" & sCMD_SEL)
			mm1 = New MenuMacro(mg, sCMD_BLUR, "^C^C" & sCMD_BLUR, "ID_" & sCMD_BLUR)
			mm1 = New MenuMacro(mg, sCMD_SHARP, "^C^C" & sCMD_SHARP, "ID_" & sCMD_SHARP)
			mm1 = New MenuMacro(mg, sCMD_DELP, "^C^C" & sCMD_DELP, "ID_" & sCMD_DELP)
			mm1 = New MenuMacro(mg, sCMD_INSP_2P, "^C^C" & sCMD_INSP_2P, "ID_" & sCMD_INSP_2P)
			mm1 = New MenuMacro(mg, sCMD_INSP_L, "^C^C" & sCMD_INSP_L, "ID_" & sCMD_INSP_L)
			mm1 = New MenuMacro(mg, sCMD_SETTINGS, "^C^C" & sCMD_SETTINGS, "ID_" & sCMD_SETTINGS)

			Dim tbBtn As ToolbarButton = Nothing
			Dim newTb As Toolbar = New Toolbar(sAPPNAME, pcs.MenuGroup)
			newTb.ElementID = "EID_TNIK_POLY"
			newTb.ToolbarOrient = ToolbarOrient.floating
			newTb.ToolbarVisible = ToolbarVisible.show
            newTb.XCoordinate = Application.MainWindow.Location.X
            newTb.YCoordinate = 100 + MIDS.POLY * 30

			tbBtn = New ToolbarButton(newTb, -1)
			tbBtn.Name = CMain._T(tx.PLY_INSP)
			tbBtn.MacroID = "ID_" & sCMD_INSP_L

			tbBtn = New ToolbarButton(newTb, -1)
			tbBtn.Name = CMain._T(tx.PLY_INSMP)
			tbBtn.MacroID = "ID_" & sCMD_INSP_2P

			tbBtn = New ToolbarButton(newTb, -1) ' separator

			tbBtn = New ToolbarButton(newTb, -1)
			tbBtn.Name = CMain._T(tx.PLY_DELP)
			tbBtn.MacroID = "ID_" & sCMD_DELP

			tbBtn = New ToolbarButton(newTb, -1) ' separator

			tbBtn = New ToolbarButton(newTb, -1)
			tbBtn.Name = CMain._T(tx.PLY_SHARP)
			tbBtn.MacroID = "ID_" & sCMD_SHARP

			tbBtn = New ToolbarButton(newTb, -1)
			tbBtn.Name = CMain._T(tx.PLY_ROUND)
			tbBtn.MacroID = "ID_" & sCMD_BLUR

			tbBtn = New ToolbarButton(newTb, -1) ' separator

			tbBtn = New ToolbarButton(newTb, -1)
			tbBtn.Name = CMain._T(tx.PLY_SELOBJINS)
			tbBtn.MacroID = "ID_" & sCMD_SEL

			tbBtn = New ToolbarButton(newTb, -1) ' separator

			tbBtn = New ToolbarButton(newTb, -1)
			tbBtn.Name = CMain._T(tx.SETTINGS)
			tbBtn.MacroID = "ID_" & sCMD_SETTINGS

#End If
        Catch ex As System.Exception
            CMain.DebugException(ex)
        End Try
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    '// the module is asked to tell it's name
    Public Shared Function GetName() As String
        Return sAPPNAME
    End Function
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    '// the module is asked to tell about itself
    Public Shared Function GetDescription() As String
        Dim sOut As String = sAPPNAME
        Try
            Dim cIni As New IniWork(CApp.sAppPath & CMain.APPSTG)
            If cIni.HasErrors = True Then Exit Try
            sOut = cIni.Read(sAPPNAME, CMain.DESCRKEY & _
                             CMain.eAppLang.ToString, sAPPNAME)
        Catch ex As System.Exception
            CMain.DebugException(ex)
        End Try
        Return sOut
    End Function
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    '// the module is asked to tell about itself
    Public Shared Sub GetInfos(ByRef sName As String, ByRef sVer As String, ByRef sTip As String)
        sName = sAPPNAME
        sVer = sAPPVER
        sTip = sTip
    End Sub
    '// ===================================================================== '

#End Region

#Region "GENERIC FUNCTIONS"

    '// --------------------------------------------------------------------- '
    Private Shared Function GetPolySel( _
        ByRef acTrans As Transaction, _
        ByRef acDoc As Document, _
        ByRef acPoly As Polyline, ByRef pkp As FSK_PT) _
        As Boolean

        Dim bRet As Boolean = False
        Try
            Dim peo As PromptEntityOptions
            peo = New PromptEntityOptions(vbLf & CMain._T(TX.SELPYTOMOD) & ": ")
            peo.SetRejectMessage(vbLf & CMain._T(TX.ERR_OBJTY) & "!")
            peo.AddAllowedClass(GetType(Autodesk.AutoCAD. _
                DatabaseServices.Polyline), False)
            Dim per As PromptEntityResult = acDoc.Editor.GetEntity(peo)
            If per.Status <> PromptStatus.OK Then Return False
            acPoly = DirectCast( _
                acTrans.GetObject(per.ObjectId, OpenMode.ForRead),  _
                      Polyline)
            bRet = True
            pkp.N_1 = per.PickedPoint.Y
            pkp.E_1 = per.PickedPoint.X
            pkp.i = 0
        Catch ex As System.Exception
            Debug.Print("Failed to select polyline: " & ex.Message)
        End Try
        Return bRet
    End Function
    '// ===================================================================== '


#End Region


#Region "C O M M A N D S"

    '// --------------------------------------------------------------------- '
    '// Command. Inserts a point in a line
    <CommandMethod(sCMD_INSP_L)> _
    Public Sub Poly_SplitLine()
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        If (acDoc Is Nothing) Then Return
        Dim acPoly As Polyline = Nothing
        Dim pkp As FSK_PT = Nothing
        Dim ppo As PromptPointOptions
        Dim ppr As PromptPointResult
        Dim iIns As Integer
        Dim iClose As Integer

        Using acTrans As Transaction = _
            acDoc.Database.TransactionManager.StartTransaction()
            Try
                Dim acBlkTblRec As BlockTableRecord = DirectCast( _
                        acTrans.GetObject(acDoc.Database.CurrentSpaceId, _
                                            OpenMode.ForWrite),  _
                        BlockTableRecord)

                '// select the polyline in question
                If (GetPolySel(acTrans, acDoc, acPoly, pkp) = False) Then
                    Exit Try
                End If

                '// prepare to ask the user
                ppo = New PromptPointOptions(vbLf & CMain._T(TX.PICKPOINT) & ":")
                ppo.UseBasePoint = False

                Do
                    '// see if the point is a proper one
                    Dim rthit As CMain.POLYPT = CMain.PolyVsPt(acPoly, pkp, iClose)
                    If (rthit = CMain.POLYPT.LINE) Then
                        iIns = pkp.i
                        Exit Do
                    ElseIf (rthit = CMain.POLYPT.OUTSIDE) Then
                        iIns = iClose
                        Exit Do
                    End If
                    CMain.AlertUser(TX.ERR_PTNOTONP)
                    '// ask the user to pick a point on the line
                    ppr = acDoc.Editor.GetPoint(ppo)
                    If ppr.Status <> PromptStatus.OK Then Exit Try
                    pkp.E_1 = ppr.Value.X
                    pkp.N_1 = ppr.Value.Y
                Loop

                '// select a location for new point
                ppo.BasePoint = New Point3d(pkp.E_1, pkp.N_1, 0)
                ppo.UseBasePoint = True
                ppo.UseDashedLine = True
                ppo.Message = CMain._T(TX.PICKPOINT)
                Do
                    ppr = acDoc.Editor.GetPoint(ppo)
                    If ppr.Status <> PromptStatus.OK Then Exit Try
                    pkp.E_1 = ppr.Value.X
                    pkp.N_1 = ppr.Value.Y
                    If (CMain.PolyVsPt(acPoly, pkp, iClose) <> CMain.POLYPT.VERTEX) Then
                        Exit Do
                    End If
                    CMain.AlertUser(TX.ESEL_PTOPLINE)
                Loop

                '// we now have the line and the position; go insert
                acPoly.UpgradeOpen()

                acPoly.AddVertexAt(iIns + 1, _
                                   New Point2d(pkp.E_1, pkp.N_1), 0, 0, 0)

                acTrans.Commit()
            Catch ex As System.Exception
                acTrans.Abort()
                CMain.DebugException(ex)
                CMain.PopUpExc()
            End Try
        End Using

        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    '// Command. Inserts multiple points between two other points
    <CommandMethod(sCMD_INSP_2P, CommandFlags.UsePickSet)> _
    Public Sub Poly_InsertBetween2Points()
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        If (acDoc Is Nothing) Then Return

        Dim acPoly As Polyline = Nothing
        Dim pt_1 As FSK_PT = Nothing
        Dim pt_2 As FSK_PT = Nothing
        Dim pt2 As Point2d
        Dim ppo As PromptPointOptions
        Dim ppr As PromptPointResult
        Dim oid As ObjectId = ObjectId.Null

        Dim iCrtIdx As Integer
        Dim iIdxChg As Integer

        Using acTrans As Transaction = _
            acDoc.Database.TransactionManager.StartTransaction()
            Try
                Dim acBlkTblRec As BlockTableRecord = DirectCast( _
                        acTrans.GetObject(acDoc.Database.CurrentSpaceId, _
                                            OpenMode.ForWrite),  _
                        BlockTableRecord)

                '// select the polyline in question
                If CMain.SelectPolyline(acTrans, acDoc, acPoly, False, True) = False Then Exit Try


                '// =====================================================
                '// Pick first point 
                With pt_1
                    ppo = New PromptPointOptions(vbLf & CMain._T(TX.FPOINT) & ":")
                    ppo.UseBasePoint = False
                    Do
                        ppr = acDoc.Editor.GetPoint(ppo)
                        If ppr.Status <> PromptStatus.OK Then Exit Try
                        .N_1 = ppr.Value.Y
                        .E_1 = ppr.Value.X
                        For .i = 0 To acPoly.NumberOfVertices - 1
                            pt2 = acPoly.GetPoint2dAt(.i)
                            If Math.Abs(ppr.Value.Y - pt2.Y) < CMain.MARGIN Then
                                If Math.Abs(ppr.Value.X - pt2.X) _
                                    < CMain.MARGIN Then Exit Do
                            End If
                        Next .i
                        CMain.AlertUser(TX.ERR_PTONL)
                    Loop
                End With
                '// =====================================================

                '// =====================================================
                '// Pick second point 
                With pt_2
                    ppo = New PromptPointOptions(vbLf & CMain._T(TX.SPOINT) & ":")
                    ppo.UseBasePoint = False
                    Do
                        ppr = acDoc.Editor.GetPoint(ppo)
                        If ppr.Status <> PromptStatus.OK Then Exit Try
                        .N_1 = ppr.Value.Y
                        .E_1 = ppr.Value.X
                        For .i = 0 To acPoly.NumberOfVertices - 1
                            pt2 = acPoly.GetPoint2dAt(.i)
                            If Math.Abs(ppr.Value.Y - pt2.Y) < CMain.MARGIN Then
                                If Math.Abs(ppr.Value.X - pt2.X) _
                                    < CMain.MARGIN Then
                                    '// it must not be the same as 
                                    '// first point
                                    If (.i = pt_1.i) Then
                                        CMain.AlertUser(TX.ERRP_ID1)
                                        Continue Do
                                    End If
                                    '// see the order
                                    If (pt_1.i = 0) Then
                                        '// we're starting from polyline start
                                        If (pt_2.i = 1) Then
                                            '// and we're ascending
                                            iCrtIdx = 1
                                            iIdxChg = 1
                                            Exit Do
                                        ElseIf (pt_2.i = acPoly.NumberOfVertices - 1) Then
                                            '// and we're descending
                                            iCrtIdx = 0
                                            iIdxChg = 0
                                            Exit Do
                                            'Else error                                        '
                                        End If
                                    ElseIf pt_1.i = acPoly.NumberOfVertices - 1 Then
                                        '// we're starting from polyline end
                                        If (pt_2.i = 0) Then
                                            '// and we're ascending (append)
                                            iCrtIdx = acPoly.NumberOfVertices
                                            iIdxChg = 1
                                            Exit Do
                                        ElseIf (pt_2.i = acPoly.NumberOfVertices - 2) Then
                                            '// and we're descending
                                            iCrtIdx = pt_1.i
                                            iIdxChg = 0
                                            Exit Do
                                            'Else error                                        '
                                        End If
                                    Else
                                        '// we're starting from inside
                                        If (pt_1.i - pt_2.i) = 1 Then
                                            '// we're descending
                                            iCrtIdx = pt_1.i
                                            iIdxChg = 0
                                            Exit Do
                                        ElseIf (pt_1.i - pt_2.i) = -1 Then
                                            '// we're ascending
                                            iCrtIdx = pt_2.i
                                            iIdxChg = 1
                                            Exit Do
                                            'Else error                                        '
                                        End If
                                    End If
                                End If
                            End If
                        Next .i
                        CMain.AlertUser(TX.ERR_PTONL)
                    Loop
                End With
                '// =====================================================

                '// we now have the line and the position; go insert
                oid = acPoly.ObjectId
                acTrans.Commit()
            Catch ex As System.Exception
                acTrans.Abort()
                CMain.DebugException(ex)
                CMain.PopUpExc()
            End Try
        End Using
        If (oid = ObjectId.Null) Then
            Return
        End If

        Do
            Using acTrans As Transaction = _
                acDoc.Database.TransactionManager.StartTransaction()
                Try
                    '// select a location for new point
                    ppo = New PromptPointOptions(vbLf & CMain._T(TX.PICKPOINT) & ":")
                    ppo.BasePoint = New Point3d(pt_1.E_1, pt_1.N_1, 0)
                    ppo.UseBasePoint = True
                    ppo.UseDashedLine = True
                    Do
                        ppr = acDoc.Editor.GetPoint(ppo)
                        If ppr.Status <> PromptStatus.OK Then
                            oid = ObjectId.Null
                            Exit Try
                        End If
                        pt_1.E_1 = ppr.Value.X
                        pt_1.N_1 = ppr.Value.Y
                        For pt_1.i = 0 To acPoly.NumberOfVertices - 1
                            pt2 = acPoly.GetPoint2dAt(pt_1.i)
                            If Math.Abs(ppr.Value.Y - pt2.Y) < CMain.MARGIN Then
                                If Math.Abs(ppr.Value.X - pt2.X) _
                                    < CMain.MARGIN Then
                                    CMain.AlertUser(TX.ESEL_PTOPLINE)
                                    Continue Do
                                End If
                            End If
                        Next pt_1.i
                        Exit Do
                    Loop

                    acPoly = acTrans.GetObject(oid, OpenMode.ForWrite)
                    acPoly.AddVertexAt(iCrtIdx, _
                                       New Point2d(pt_1.E_1, pt_1.N_1), 0, 0, 0)
                    iCrtIdx = iCrtIdx + iIdxChg
                    acTrans.Commit()
                Catch ex As System.Exception
                    acTrans.Abort()
                    CMain.DebugException(ex)
                    CMain.PopUpExc()
                    oid = ObjectId.Null
                End Try
            End Using
        Loop Until (oid = ObjectId.Null)

        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    '// Command. Eliminate points from polyline
    <CommandMethod(sCMD_DELP, CommandFlags.UsePickSet)> _
    Public Sub Poly_DelPoint()
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        If (acDoc Is Nothing) Then Return

        Dim acPoly As Polyline = Nothing
        Dim pt_1 As FSK_PT = Nothing
        Dim pt_2 As FSK_PT = Nothing
        Dim pt2 As Point2d
        Dim ppo As PromptPointOptions
        Dim ppr As PromptPointResult
        Dim oid As ObjectId = ObjectId.Null

        Using acTrans As Transaction = _
            acDoc.Database.TransactionManager.StartTransaction()
            Try
                Dim acBlkTblRec As BlockTableRecord = DirectCast( _
                        acTrans.GetObject(acDoc.Database.CurrentSpaceId, _
                                            OpenMode.ForWrite),  _
                        BlockTableRecord)

                '// select the polyline in question
                If CMain.SelectPolyline(acTrans, acDoc, acPoly, False, True) = False Then Exit Try




                oid = acPoly.ObjectId
                acTrans.Commit()
            Catch ex As System.Exception
                acTrans.Abort()
                CMain.DebugException(ex)
                CMain.PopUpExc()
            End Try
        End Using
        If (oid = ObjectId.Null) Then
            GC.Collect()
            GC.WaitForPendingFinalizers()
            Return
        End If

        Do
            Using acTrans As Transaction = _
                acDoc.Database.TransactionManager.StartTransaction()
                Try
                    acPoly = acTrans.GetObject(oid, OpenMode.ForWrite)

                    '// select a point
                    ppo = New PromptPointOptions(vbLf & CMain._T(TX.PICKPOINT) & ":")
                    ppo.UseBasePoint = False
                    ppo.UseDashedLine = True
                    Do
                        ppr = acDoc.Editor.GetPoint(ppo)
                        If ppr.Status <> PromptStatus.OK Then
                            acTrans.Commit()
                            oid = ObjectId.Null
                            Exit Try
                        End If
                        pt_1.E_1 = ppr.Value.X
                        pt_1.N_1 = ppr.Value.Y
                        For pt_1.i = 0 To acPoly.NumberOfVertices - 1
                            pt2 = acPoly.GetPoint2dAt(pt_1.i)
                            If Math.Abs(ppr.Value.Y - pt2.Y) < CMain.MARGIN Then
                                If Math.Abs(ppr.Value.X - pt2.X) _
                                    < CMain.MARGIN Then
                                    Exit Do
                                End If
                            End If
                        Next pt_1.i
                        CMain.AlertUser(TX.ERR_PTNOTONP)
                    Loop

                    If acPoly.NumberOfVertices = 2 Then
                        acPoly.Erase()
                        oid = ObjectId.Null
                        acTrans.Commit()
                        Exit Try
                    End If
                    acPoly.RemoveVertexAt(pt_1.i)
                    acTrans.Commit()
                Catch ex As System.Exception
                    acTrans.Abort()
                    CMain.DebugException(ex)
                    CMain.PopUpExc()
                    oid = ObjectId.Null
                End Try
            End Using
        Loop Until (oid = ObjectId.Null)
        GC.Collect()
        GC.WaitForPendingFinalizers()
        Return
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    '// Command. Sharpen a polyline (reduce point number)
    <CommandMethod(sCMD_SHARP)> _
    Public Sub Poly_Sharp()
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        If (acDoc Is Nothing) Then Return
        Dim acTrans As Transaction = _
            acDoc.Database.TransactionManager.StartTransaction()
        Try
            Dim acBlkTblRec As BlockTableRecord = DirectCast( _
                    acTrans.GetObject(acDoc.Database.CurrentSpaceId, _
                                        OpenMode.ForWrite),  _
                    BlockTableRecord)




            CMain.AlertUser("This command is not fully implemented yet")

            acTrans.Commit()
        Catch ex As System.Exception
            acTrans.Abort()
            CMain.DebugException(ex)
            CMain.PopUpExc()
        End Try
        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    '// Command. Blur a polyline (increase point number)
    <CommandMethod(sCMD_BLUR)> _
    Public Sub Poly_Blur()
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        If (acDoc Is Nothing) Then Return
        Dim acTrans As Transaction = _
            acDoc.Database.TransactionManager.StartTransaction()
        Try
            Dim acBlkTblRec As BlockTableRecord = DirectCast( _
                    acTrans.GetObject(acDoc.Database.CurrentSpaceId, _
                                        OpenMode.ForWrite),  _
                    BlockTableRecord)

            CMain.AlertUser("This command is not fully implemented yet")

            acTrans.Commit()
        Catch ex As System.Exception
            acTrans.Abort()
            CMain.DebugException(ex)
            CMain.PopUpExc()
        End Try
        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    '// Command. Blur a polyline (increase point number)
    <CommandMethod(sCMD_SEL)> _
    Public Sub Poly_Select()
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        If (acDoc Is Nothing) Then Return
        Dim acTrans As Transaction = _
            acDoc.Database.TransactionManager.StartTransaction()
        Try
            Dim acBlkTblRec As BlockTableRecord = DirectCast( _
                    acTrans.GetObject(acDoc.Database.CurrentSpaceId, _
                                        OpenMode.ForWrite),  _
                    BlockTableRecord)

            CMain.AlertUser("This command is not fully implemented yet")

            acTrans.Commit()
        Catch ex As System.Exception
            acTrans.Abort()
            CMain.DebugException(ex)
            CMain.PopUpExc()
        End Try
        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Sub
    '// ===================================================================== '

    '// --------------------------------------------------------------------- '
    <CommandMethod(sCMD_SETTINGS)> _
    Public Sub Show_Settings()
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        If (acDoc Is Nothing) Then Return
        Try

            Dim StgD As CST_Polyline = New CST_Polyline
            Application.ShowModelessDialog(StgD)
        Catch ex As System.Exception
            CMain.DebugException(ex)
            CMain.PopUpExc()
        End Try
        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Sub
    '// ===================================================================== '

#End Region

End Class
'//  CLASS    =============================================================== '
'//
'//
'//
'//
'//  FUNCTIONS    ----------------------------------------------------------- '

'//  FUNCTIONS    =========================================================== '
'//
'//
'//
'//
'// ------------------------------------------------------------------------- '
'// ========================================================================= '
